import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shop/app/modules/cart/views/cart_item_view.dart';
import 'package:shop/tools/screenAdapter.dart';

import '../controllers/cart_controller.dart';

class CartView extends GetView {
  //注意CartView在多个地方调用了  需要手动获取CartController
  @override
  final CartController controller = Get.put(CartController());

  CartView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: const Text('购物车'),
        centerTitle: true,
        actions: [TextButton(onPressed: () {}, child: Text('编辑'))],
      ),
      body: GetBuilder<CartController>(
        initState: (state) {
          //获取购物车数据
          controller.getCartListData();
        },
        init: controller,
        builder: (controller) {
          return controller.cartList.isNotEmpty
              ? Stack(
                  children: [
                    ListView(
                      padding: EdgeInsets.only(
                        bottom: ScreenAdapter.height(200),
                      ),
                      children: controller.cartList.map((value) {
                        return CartItemView(value);
                      }).toList(),
                    ),
                    Positioned(
                      left: 0,
                      bottom: 0,
                      right: 0,
                      child: Container(
                        padding: EdgeInsets.only(
                          right: ScreenAdapter.width(20),
                        ),
                        width: ScreenAdapter.width(1080),
                        height: ScreenAdapter.height(190),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                            top: BorderSide(
                              color: Colors.black38,
                              width: ScreenAdapter.height(2),
                            ),
                          ),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Checkbox(
                                  activeColor: Colors.red,
                                  value: controller.checkAllBox.value,
                                  onChanged: (value) {
                                    controller.selectAllShop(value);
                                  },
                                ),
                                Text('全选'),
                              ],
                            ),
                            Row(
                              children: [
                                Text('合计: '),
                                Text(
                                  '￥${controller.selectTotalPrice}',
                                  style: TextStyle(
                                    fontSize: ScreenAdapter.fontSize(58),
                                    color: Colors.red,
                                  ),
                                ),
                                SizedBox(width: ScreenAdapter.width(20)),
                                ElevatedButton(
                                  style: ButtonStyle(
                                    backgroundColor: WidgetStatePropertyAll(
                                      Color.fromRGBO(255, 165, 0, 0.9),
                                    ),
                                    foregroundColor: WidgetStatePropertyAll(
                                      Colors.white,
                                    ),
                                    shape: WidgetStatePropertyAll(
                                      RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                    ),
                                  ),
                                  onPressed: () {},
                                  child: Text('结算'),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                )
              : Center(child: Text('购物车空空的'));
        },
      ),
    );
  }
}
